package com.wannengbang.cloudleader.homepage;

import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ethanhua.skeleton.Skeleton;
import com.ethanhua.skeleton.SkeletonScreen;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.wannengbang.cloudleader.R;
import com.wannengbang.cloudleader.base.BaseActivity;
import com.wannengbang.cloudleader.base.DataCallBack;
import com.wannengbang.cloudleader.bean.ProductListBean;
import com.wannengbang.cloudleader.homepage.adapter.ProductListAdapter;
import com.wannengbang.cloudleader.homepage.model.HomePageModelImpl;
import com.wannengbang.cloudleader.homepage.model.IHomePageModel;
import com.wannengbang.cloudleader.utils.DisplayUtil;
import com.wannengbang.cloudleader.utils.Dp2Px;
import com.wannengbang.cloudleader.utils.GlideUtils;
import com.wannengbang.cloudleader.widget.AppTitleBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ProductListActivity extends BaseActivity implements OnRefreshLoadMoreListener {

    @BindView(R.id.title_bar)
    AppTitleBar titleBar;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.iv_photo)
    ImageView ivPhoto;
    private IHomePageModel homePageModel;
    private ProductListAdapter listAdapter;
    private List<ProductListBean.DataBean.ItemListBean> beanList;
    private SkeletonScreen skeletonScreen;
    private int page = 1;
    private String type;
    private String typeName;
    private String pic_info;
    int mHeight = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_list);
        ButterKnife.bind(this);
        initView();
        requestGoodsIndex();
    }

    public void initView() {
        type = getIntent().getStringExtra("type");
        typeName = getIntent().getStringExtra("typeName");
        pic_info = getIntent().getStringExtra("pic_info");

        mHeight = (int) ((DisplayUtil.getScreenWidth() - 30) * 262 / 668);
        ViewGroup.LayoutParams params = ivPhoto.getLayoutParams();
        params.height = mHeight;
        GlideUtils.loadImage(mActivity, ivPhoto, pic_info);

        titleBar.setTitle(typeName);
        refreshLayout.setRefreshHeader(new ClassicsHeader(mActivity));//设置Header
        refreshLayout.setRefreshFooter(new ClassicsFooter(mActivity));//设置Footer
        refreshLayout.setOnRefreshLoadMoreListener(this);
        homePageModel = new HomePageModelImpl();
        beanList = new ArrayList<>();
        listAdapter = new ProductListAdapter(beanList);
        recyclerView.setLayoutManager(new GridLayoutManager(mActivity, 2));
        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                int i = parent.getChildLayoutPosition(view) % 2;//每行2个
                switch (i) {
                    case 0://第一个
                        outRect.left = (int) Dp2Px.dp2Px(16);
                        outRect.right = (int) Dp2Px.dp2Px(6);
                        break;
                    case 1://第二个
                        outRect.left = (int) Dp2Px.dp2Px(6);
                        outRect.right = (int) Dp2Px.dp2Px(16);
                        break;
                }
            }
        });
        recyclerView.setAdapter(listAdapter);
        listAdapter.setOnCallBackListener(new ProductListAdapter.onCallBackListener() {
            @Override
            public void onCallBack(int type, int position) {

                //购买
                if (1 == type) {
                    Intent intent = new Intent(mActivity, ConfirmOrderActivity.class);
                    intent.putExtra("ItemListBean", beanList.get(position));
                    startActivity(intent);
                }
            }
        });
        skeletonScreen = Skeleton.bind(recyclerView)
                .adapter(listAdapter)
                .color(R.color.shimmer_color_2)
                .shimmer(true)
                .angle(20)
                .frozen(false)
                .duration(1000)
                .count(20)
                .load(R.layout.item_skeleton_product_list)
                .show();
    }

    private void requestGoodsIndex() {
        homePageModel.requestGoodsIndex(page, type, new DataCallBack<ProductListBean>() {
            @Override
            public void onSuccessful(ProductListBean bean) {
                if (bean.getData().getItemList() != null && bean.getData().getItemList().size() > 0) {
                    beanList.addAll(bean.getData().getItemList());
                    if (page == 1) {
                        refreshLayout.finishRefresh();
                    } else {
                        refreshLayout.finishLoadMore();
                    }
                } else {
                    if (page != 1) {
                        refreshLayout.finishLoadMoreWithNoMoreData();
                    } else {
                        refreshLayout.finishRefresh();
                    }
                }
                if (beanList.size() > 0) {
                    llNoData.setVisibility(View.GONE);
                } else {
                    llNoData.setVisibility(View.VISIBLE);
                }
                listAdapter.notifyDataSetChanged();
                if (page == 1) {
                    skeletonScreen.hide();
                }
            }

            @Override
            public void onFailed(String msg, String code) {
                skeletonScreen.hide();
            }
        });
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        page++;
        requestGoodsIndex();
        refreshLayout.finishLoadMore(30000);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        refreshData();
    }

    public void refreshData() {
        page = 1;
        beanList.clear();
        requestGoodsIndex();
        refreshLayout.finishRefresh(30000);
    }
}
